# Maintainer: David Macek <david.macek.0@gmail.com>

# TODO: include more stuff from tools/?

_realname=nim
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=1.2.0
_csourcesver=0.20.0
pkgrel=1
pkgdesc='Imperative, multi-paradigm, compiled programming language (mingw-w64)'
arch=('any')
url='https://nim-lang.org/'
license=('MIT')
makedepends=('git')
options=(!emptydirs)
source=("${_realname}-${pkgver}.tar.gz"::https://github.com/nim-lang/Nim/archive/v${pkgver}.tar.gz
        "${_realname}-csources-${_csourcesver}.tar.gz"::https://github.com/nim-lang/csources/archive/v${_csourcesver}.tar.gz
        "0001-Use-unixy-filenames-even-on-Windows.patch"
        "0002-Fix-config-path.patch"
        "0003-Fix-library-path.patch"
        "0004-Fix-nimrtl-dll-name.patch"
        "0005-Fix-pcre-dll-name.patch"
        "0006-Fix-sqlite3-dll-name.patch")
sha256sums=('0920fe06b724de03ace28ec63db2e71734c1ab77499cafe08289bce25be8d8a4'
            '5e6fd15d90df1a8cb7614c4ffc70aa8c4198cd854d7742016202b96dd0228d3c'
            '3010acbe7769ea214dd51f5889e3a26b44925001905deb04b060beaede2578f4'
            'e4616080589f8140dbf4e4dc8afff1dddd95c93af51e2754bd84606cee415386'
            'e66e869c7f39a0fa5fa98512ae1d2a02befface3ff2d4d36abc2b63616e24039'
            '9d3efcc253986708cac461c8bd691b0edf660f63be5d77b85253402899ef784f'
            'fa475808c100a86174ae348faaa4048d09e56406cb54c2f795da9d733605b003'
            '008a090cf561124796daaab8390f495bc740a4ec8936eb96bc025cee6e017e3d')

prepare() {
  cd "${srcdir}"

  rm -rf "${_realname}-${pkgver}/build"
  cp -R "csources-${_csourcesver}" "${_realname}-${pkgver}/build"

  cd "${_realname}-${pkgver}"
  patch -p1 -i "${srcdir}/0001-Use-unixy-filenames-even-on-Windows.patch"
  patch -p1 -i "${srcdir}/0002-Fix-config-path.patch"
  patch -p1 -i "${srcdir}/0003-Fix-library-path.patch"
  patch -p1 -i "${srcdir}/0004-Fix-nimrtl-dll-name.patch"
  patch -p1 -i "${srcdir}/0005-Fix-pcre-dll-name.patch"
  patch -p1 -i "${srcdir}/0006-Fix-sqlite3-dll-name.patch"
}

build() {
  cd "${srcdir}/${_realname}-${pkgver}"

  msg2 "Building nim"
  pushd build
  sh build.sh --cpu ${MINGW_CHOST}
  popd

  msg2 "Building koch"
  ./bin/nim c -d:release koch
  ./koch boot -d:release -d:useGnuReadline

  export PATH="${srcdir}/${_realname}-${pkgver}/bin:$PATH"

  msg2 "Building libs"
  pushd lib
  nim c --app:lib -d:createNimRtl -d:release nimrtl.nim
  popd

  msg2 "Building tools"
  pushd tools
  nim c -d:release nimgrep.nim
  popd

  msg2 "Building nimsuggest"
  nim c -d:release nimsuggest/nimsuggest.nim
}

package() {
  cd "${srcdir}/${_realname}-${pkgver}"

  export PATH="${srcdir}/${_realname}-${pkgver}/bin:$PATH"

  ./koch install "${pkgdir}"

  mkdir -p "${pkgdir}${MINGW_PREFIX}/lib"
  cp -a lib "${pkgdir}${MINGW_PREFIX}/lib/nim"
  cp -a compiler "${pkgdir}${MINGW_PREFIX}/lib/nim"

  cp "compiler.nimble" "${pkgdir}${MINGW_PREFIX}/lib/nim/compiler"

  mkdir -p "${pkgdir}${MINGW_PREFIX}/bin"
  cp "lib/libnimrtl.dll" "${pkgdir}${MINGW_PREFIX}/bin"

  install -Dm 644 config/* -t "${pkgdir}${MINGW_PREFIX}/etc/nim"
  install -Dm 755 bin/* tools/nimgrep nimsuggest/nimsuggest -t "${pkgdir}${MINGW_PREFIX}/bin"

  # Fix FS#50252, unusual placement of header files
  install -d "${pkgdir}${MINGW_PREFIX}/include"
  cp -a "${pkgdir}${MINGW_PREFIX}/lib/nim/"*.h "${pkgdir}${MINGW_PREFIX}/include"

  mkdir -p "${pkgdir}${MINGW_PREFIX}/share/nim/doc"
  cp -r "examples" "${pkgdir}${MINGW_PREFIX}/share/nim/doc/"
  cp -r "doc"/* "${pkgdir}${MINGW_PREFIX}/share/nim/doc/"
  mkdir -p "${pkgdir}${MINGW_PREFIX}/lib/nim/doc"
  cp doc/{basicopt.txt,advopt.txt} "${pkgdir}${MINGW_PREFIX}/lib/nim/doc/"

  mkdir -p "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}"
  cp "copying.txt" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"

  # clean up after koch install
  rm -r "${pkgdir}/nim"
  rm -f ${pkgdir}${MINGW_PREFIX}/bin/*.txt
  rm -r ${pkgdir}${MINGW_PREFIX}/lib/nim/*.dll
  rm -r ${pkgdir}${MINGW_PREFIX}/lib/nim/compiler/*.exe
}
